package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.AccountOpenAuth;
import com.xhwl.data.dao.IAccountOpenAuthDao;
import com.xhwl.data.service.IAccountOpenAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author:
 * @create: 2020-11-25
 **/
@Service
public class AccountOpenAuthServiceImpl extends ServiceImpl<IAccountOpenAuthDao, AccountOpenAuth> implements IAccountOpenAuthService {
    private static final Logger log = LoggerFactory.getLogger(AccountOpenAuthServiceImpl.class);
    @Autowired
    IAccountOpenAuthDao accountOpenAuthDao;

    @Override
    public ResultJson create(AccountOpenAuth accountOpenAuth) {
        QueryWrapper<AccountOpenAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountOpenAuth.getAccountId());
        queryWrapper.eq("open_id", accountOpenAuth.getOpenId());
        queryWrapper.eq("type", accountOpenAuth.getType());
        queryWrapper.eq("app_id", accountOpenAuth.getAppId());
        AccountOpenAuth checkDb = accountOpenAuthDao.selectOne(queryWrapper);
        if (null != checkDb) {
            log.info("该账号/微信已经绑定,无需重复绑定,账号id={},openId={}", accountOpenAuth.getAccountId(), accountOpenAuth.getOpenId());
            return ResultJson.success("绑定成功");
        }
        accountOpenAuthDao.insert(accountOpenAuth);
        return ResultJson.success("绑定成功");
    }

    @Override
    public AccountOpenAuth getOne(AccountOpenAuth accountOpenAuth) {
        QueryWrapper<AccountOpenAuth> queryWrapper = new QueryWrapper<>();
        if (null != accountOpenAuth.getOpenId()) {
            queryWrapper.eq("open_id", accountOpenAuth.getOpenId());
        }
        if (null != accountOpenAuth.getAccountId()) {
            queryWrapper.eq("account_id", accountOpenAuth.getAccountId());
        }
        if (null != accountOpenAuth.getType()) {
            queryWrapper.eq("type", accountOpenAuth.getType());
        }
        AccountOpenAuth openAuth = null;
        String appId = accountOpenAuth.getAppId();
        if (!StringUtils.isEmpty(appId)) {
            queryWrapper.eq("app_id", appId);
            openAuth = accountOpenAuthDao.selectOne(queryWrapper);
        } else {
            //兼容没有appId的老记录
            List<AccountOpenAuth> accountOpenAuths = accountOpenAuthDao.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(accountOpenAuths)) {
                openAuth = accountOpenAuths.get(0);
            }
        }
        return openAuth;
    }

    @Override
    public List<AccountOpenAuth> listBindOpen(Integer accountId, String appId) {
        QueryWrapper<AccountOpenAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        if (!StringUtils.isEmpty(appId)) {
            queryWrapper.eq("app_id", appId);
        }
        return accountOpenAuthDao.selectList(queryWrapper);
    }


    @Override
    public ResultJson update(AccountOpenAuth accountOpenAuth) {
        accountOpenAuthDao.updateById(accountOpenAuth);
        return ResultJson.success("修改成功");
    }

    @Override
    public ResultJson delete(AccountOpenAuth accountOpenAuth) {
        List<AccountOpenAuth> accountOpenAuths = new ArrayList<>();
        //openId唯一时，记录唯一。  否则需要 accountId、type、appId都不为空；
        if (StringUtils.isEmpty(accountOpenAuth.getOpenId()) && (StringUtils.isEmpty(accountOpenAuth.getAccountId()) || StringUtils.isEmpty(accountOpenAuth.getType()) || StringUtils.isEmpty(accountOpenAuth.getAppId()))) {
            accountOpenAuths = listBindOpen(accountOpenAuth.getAccountId(), accountOpenAuth.getAppId());
        } else {
            accountOpenAuths.add(getOne(accountOpenAuth));
        }
        if (!CollectionUtils.isEmpty(accountOpenAuths)) {
            List<Integer> collect = accountOpenAuths.stream().map(accountOpenAuth1 -> accountOpenAuth1.getId())
                    .collect(Collectors.toList());
            accountOpenAuthDao.deleteBatchIds(collect);
            return ResultJson.success("解绑成功");
        } else {
            log.info("解绑失败，找不到绑定的微信关系,accountOpenAuth={}", accountOpenAuth.toString());
            return ResultJson.fail("未找到当前账号与微信号的绑定数据，操作失败");
        }

    }

}
